package com.genius.human.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.ResponseJson;


import com.genius.human.service.HumanService;
import com.genius.workflow.log.ControllerLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;


/*
* author  : 67
* */


@Controller
public class humanController {
    @Autowired
    HumanService humanService;

    /**
     * 跳转人事管理列表页面

     * @return
     * @throws Exception
     */
    @RequiresPermissions("5e68cc3e00ae426da7a7e8fcd07b1389")
    @GetMapping("/human/menu/toMenuList")
    public String toHumanList(){
        return "/views/human/select";
    }


    @ControllerLog(description="获取所有人员信息")
    @PostMapping("/human/menu/menuListBody")
    @ResponseBody
    public JSONObject getAllUserInfo(@RequestBody JSONObject field) throws SQLException {
        JSONArray allUserInformation;
        String search = field.getString("search");
        if (search!=null){
            JSONObject searchUser = humanService.getSearchUser(field);
            return searchUser;
        }
        allUserInformation = humanService.getAllUserInformation(field);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("data",allUserInformation);
        jsonObject.put("status",1002);
        return jsonObject;
    }





    /**
     * 跳转到菜单编辑页面
     */
    @GetMapping("/human/menu/toEdit")
    public String toEditMenu(){
        return "/views/human/edit";
    }
//    @PostMapping("/human/menu/edit")
//    @ResponseBody
//    public JSONObject editMenu(@RequestBody JSONObject json, HttpSession session){
//        JSONObject response = new JSONObject();
//        response.put(ResponseJson.code, 0);
////        response = groupEditService.editUser(json, response);
//        return response;
//    }



    @PostMapping("/human/menu/getUser")
    @ResponseBody
    public JSONObject getUser(@RequestBody JSONObject json, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        JSONObject id = humanService.getUser(json.getString("id"));
        jsonObject.put("data",id);
        jsonObject.put("status",1002);
        return jsonObject;
    }

//    @GetMapping("/human/menu/gongzuo")
//    public String toGongzuo(){
//        return "/views/human/gongzuojilu";
//    }

    @PostMapping("/human/menu/getGongzuo")
    @ResponseBody
    public JSONObject getGongzuo(@RequestBody JSONObject json, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        JSONArray user_id = humanService.getGongzuo(json.getString("user_id"));
        jsonObject.put("data",user_id);
        jsonObject.put("status",1002);
        return jsonObject;
    }




    @PostMapping("/human/menu/upUser")
    @ResponseBody
    public JSONObject upUser(@RequestBody JSONObject json, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        humanService.updateUser(json);
        jsonObject.put("status",1002);
        jsonObject.put("msg","更新成功");
        return jsonObject;
    }

    @PostMapping("/human/menu/toInsert")
    public String toInsert(){
        return "/views/human/insert";
    }

    @PostMapping("/human/menu/insert")
    @ResponseBody
    public JSONObject insert(@RequestBody JSONObject json, HttpSession session){
        JSONArray list = json.getJSONArray("list");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        int insert = humanService.insert(json, list);
        if (insert!=0) {
            jsonObject.put("status",1003);
            jsonObject.put("msg","添加数据失败");
        }else {
            jsonObject.put("status",1002);
            jsonObject.put("msg","添加数据成功");
        }
        return jsonObject;
    }


//    @PostMapping("/human/menu/delete")
//    @ResponseBody
//    public JSONObject delete(@RequestBody JSONObject json, HttpSession session){
//        System.out.println(json);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("code",0);
//        int insert = humanService.insert(json, null);
//        System.out.println(insert);
//        if (insert!=0) {
//            jsonObject.put("status",1003);
//            jsonObject.put("msg","添加数据失败");
//        }else {
//            jsonObject.put("status",1002);
//            jsonObject.put("msg","添加数据成功");
//        }
//        return jsonObject;
//
//    }
}
